import Vue from 'vue'
import VueRouter from 'vue-router'
// import store from '../store'
import showPageFooterUriFn from './hooks/pagefooter'
import checkloginFn from './hooks/checklogin'

// 自动路由 路由模块
let moduleFn = require.context('./routes', false, /\.js$/)
let routes = moduleFn.keys().reduce((p, c) => {
  let value = moduleFn(c).default
  if (value) {
    // 路由有数组和对象两种可能，所以要判断  数组类型判断
    // in 直接.方法 instanceof Object.prototype.toString.call(变量)
    if ('push' in value) {
      p.push(...value)
    } else {
      p.push(value)
    }
  }
  return p
}, []);

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  // 访问的基础路径前缀 (默认 /)   http://www.xxx.com/home
  base: process.env.BASE_URL,
  routes
})

/* // 要把需要显示路由地址确定
let showPageFooterUri = ['/films', '/cinemas', '/news', '/center']

// router.app.$store 可以得到当前项目中的vuex对象
router.beforeEach((to, from, next) => {

  let bool = showPageFooterUri.some(item => to.path.startsWith(item))
  store.commit('setPageFooter', bool)
  next()
});
 */
router.beforeEach(showPageFooterUriFn(router))
router.beforeEach(checkloginFn)


export default router
